package com.itheima.api.pms;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.pms.*;
import com.itheima.vo.domain.AjaxResult;
import com.itheima.vo.domain.TableDataInfo;
import com.itheima.vo.dto.pms.PmsProductDto;
import com.itheima.vo.pojo.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@DubboService
public class PmsProductServiceImpl extends ServiceImpl<PmsProductMapper, PmsProduct> implements PmsProductService {

    @Autowired
    private PmsProductMapper pmsProductMapper;

    @Autowired
    private PmsMemberPriceService pmsMemberPriceService;

    @Autowired
    private CmsPrefrenceAreaProductRelationService cmsPrefrenceAreaProductRelationService;

    @Autowired
    private PmsProductAttributeValueService pmsProductAttributeValueService;

    @Autowired
    private PmsProductFullReductionService pmsProductFullReductionService;

    @Autowired
    private PmsProductLadderService pmsProductLadderService;

    @Autowired
    private PmsSkuStockService pmsSkuStockService;

    @Autowired
    private CmsSubjectProductRelationService cmsSubjectProductRelationService;

    /**
     * 1.创建商品
     *
     * @param pmsProductDto
     * @return
     */
    @Override
    @Transactional
    public AjaxResult create(PmsProductDto pmsProductDto) {
        //保存基本PmsProduct信息
        PmsProduct pmsProduct = new PmsProduct();
        BeanUtils.copyProperties(pmsProductDto, pmsProduct);
        pmsProductMapper.insert(pmsProduct);
        //保存得到新建出来的id
        long pid = pmsProduct.getId();

        //下面这些数据都有productId
        boolean b1 = pmsMemberPriceService.saveById(pid, pmsProductDto.getMemberPriceList());
        boolean b2 = cmsPrefrenceAreaProductRelationService.saveById(pid, pmsProductDto.getPrefrenceAreaProductRelationList());
        boolean b3 = pmsProductAttributeValueService.saveById(pid, pmsProductDto.getProductAttributeValueList());
        boolean b4 = pmsProductFullReductionService.saveById(pid, pmsProductDto.getProductFullReductionList());
        boolean b5 = pmsProductLadderService.saveById(pid, pmsProductDto.getProductLadderList());
        boolean b6 = pmsSkuStockService.saveById(pid, pmsProductDto.getSkuStockList());
        boolean b7 = cmsSubjectProductRelationService.saveById(pid, pmsProductDto.getSubjectProductRelationList());

        boolean b = b1 & b2 & b3 & b4 & b5 & b6 & b7;

        return AjaxResult.success("添加成功");
    }

    /**
     * 2.查询商品
     *
     * @param brandId           商品品牌编号
     * @param keyword           商品名称模糊关键字商品名称模糊关键字
     * @param pageNum
     * @param pageSize
     * @param productCategoryId 商品分类编号
     * @param productSn         商品货号
     * @param publishStatus     上架状态
     * @param verifyStatus      审核状态
     * @return
     */
    @Override
    public AjaxResult FindBylist(Integer brandId, String keyword, Integer pageNum, Integer pageSize,
                                 Integer productCategoryId, String productSn, Integer publishStatus, Integer verifyStatus) {

        LambdaQueryWrapper<PmsProduct> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ObjectUtil.isNotNull(brandId), PmsProduct::getBrandId, brandId).
                like(ObjectUtil.isNotNull(keyword), PmsProduct::getName, keyword).
                eq(ObjectUtil.isNotNull(productCategoryId), PmsProduct::getProductCategoryId, productCategoryId).
                eq(ObjectUtil.isNotNull(productSn), PmsProduct::getProductSn, productSn).
                eq(ObjectUtil.isNotNull(publishStatus), PmsProduct::getPublishStatus, publishStatus).
                eq(ObjectUtil.isNotNull(verifyStatus), PmsProduct::getVerifyStatus, verifyStatus).
                //标记删除的,0未删除,1已删除
                eq(PmsProduct::getDeleteStatus, 0);

        //构造分页对象
        Page<PmsProduct> pmsProductPage = new Page<>(pageNum, pageSize);
        Page<PmsProduct> page1 = pmsProductMapper.selectPage(pmsProductPage, lqw);

        //封装对象
        TableDataInfo tInfo = new TableDataInfo();
        tInfo.setList(page1.getRecords());
        tInfo.setPageNum(pageNum);
        tInfo.setPageSize(pageSize);
        tInfo.setTotal(page1.getTotal());
        tInfo.setTotalPage(page1.getPages());
        return AjaxResult.success(tInfo);
    }

    /**
     * 3.根据商品名称或货号模糊查询
     *
     * @param keyword
     * @return
     */
    @Override
    public AjaxResult simpleList(String keyword) {
        LambdaQueryWrapper<PmsProduct> lqw = new LambdaQueryWrapper<>();
        lqw.like(ObjectUtil.isNotNull(keyword), PmsProduct::getKeywords, keyword);

        List<PmsProduct> pmsProducts = pmsProductMapper.selectList(lqw);

        return AjaxResult.success(pmsProducts);
    }

    /**
     * 4.批量修改删除状态  0是未删除,1是已删除
     *
     * @param deleteStatus
     * @param ids
     * @return
     */
    @Override
    public AjaxResult deleteStatus(Integer deleteStatus, ArrayList<Integer> ids) {
        for (Integer id : ids) {
            //构建分页查询对象
            // LambdaQueryWrapper<PmsProduct> lqw = new LambdaQueryWrapper<>();
            // lqw.eq(PmsProduct::getId,id);
            PmsProduct pmsProduct = pmsProductMapper.selectById(id);
            pmsProduct.setDeleteStatus(deleteStatus);
            pmsProductMapper.updateById(pmsProduct);
        }
        return AjaxResult.success("批量修改删除状态成功");
    }

    /**
     * 5.批量设为新品
     *
     * @param newStatus
     * @param ids
     * @return
     */
    @Override
    public AjaxResult newStatus(Integer newStatus, ArrayList<Integer> ids) {
        for (Integer id : ids) {
            PmsProduct pmsProduct = pmsProductMapper.selectById(id);
            pmsProduct.setNewStatus(newStatus);
            pmsProductMapper.updateById(pmsProduct);
        }
        return AjaxResult.success("批量设为新品成功");
    }

    /**
     * 6.批量上下架商品
     *
     * @param publishStatus
     * @param ids
     * @return
     */
    @Override
    public AjaxResult publishStatus(Integer publishStatus, ArrayList<Integer> ids) {
        for (Integer id : ids) {
            PmsProduct pmsProduct = pmsProductMapper.selectById(id);
            pmsProduct.setPublishStatus(publishStatus);
            pmsProductMapper.updateById(pmsProduct);
            log.info(pmsProduct.toString());
        }
        return AjaxResult.success("批量上下架商品成功");
    }

    /**
     * 7.批量推荐商品
     *
     * @param recommendStatus
     * @param ids
     * @return
     */
    @Override
    public AjaxResult recommendStatus(Integer recommendStatus, ArrayList<Integer> ids) {
        for (Integer id : ids) {
            PmsProduct pmsProduct = pmsProductMapper.selectById(id);
            pmsProduct.setRecommandStatus(recommendStatus);
            pmsProductMapper.updateById(pmsProduct);
        }
        return AjaxResult.success("批量推荐商品成功");
    }

    /**
     * 8.批量修改审核状态
     *
     * @param detail
     * @param verifyStatus
     * @param ids
     * @return
     */
    @Override
    public AjaxResult verifyStatus(String detail, Integer verifyStatus, ArrayList<Integer> ids) {
        for (Integer id : ids) {
            PmsProduct pmsProduct = pmsProductMapper.selectById(id);
            pmsProduct.setVerifyStatus(verifyStatus);
            pmsProduct.setDetailDesc(detail);
            pmsProduct.setDetailHtml(detail);
            pmsProduct.setDetailMobileHtml(detail);
            pmsProduct.setDetailTitle(detail);
            pmsProductMapper.updateById(pmsProduct);
        }
        return AjaxResult.success("批量修改审核状态成功");
    }

    /**
     * 9.更新商品
     *
     * @param id
     * @param pmsProductDto
     * @return
     */
    @Override
    public AjaxResult updateWithId(Integer id, PmsProductDto pmsProductDto) {

        //更新基本PmsProduct信息
        PmsProduct pmsProduct = new PmsProduct();
        BeanUtils.copyProperties(pmsProductDto, pmsProduct);
        LambdaQueryWrapper<PmsProduct> lqw = new LambdaQueryWrapper<>();
        lqw.eq(PmsProduct::getId, id);
        pmsProductMapper.update(pmsProduct, lqw);


        //下面这些数据都有productId
        boolean b1 = pmsMemberPriceService.updateWithId(id, pmsProductDto.getMemberPriceList());
        boolean b2 = cmsPrefrenceAreaProductRelationService.updateWithId(id, pmsProductDto.getPrefrenceAreaProductRelationList());
        boolean b3 = pmsProductAttributeValueService.updateWithId(id, pmsProductDto.getProductAttributeValueList());
        boolean b4 = pmsProductFullReductionService.updateWithId(id, pmsProductDto.getProductFullReductionList());
        boolean b5 = pmsProductLadderService.updateWithId(id, pmsProductDto.getProductLadderList());
        boolean b6 = pmsSkuStockService.updateWithId(id, pmsProductDto.getSkuStockList());
        boolean b7 = cmsSubjectProductRelationService.updateWithId(id, pmsProductDto.getSubjectProductRelationList());

        boolean b = b1 & b2 & b3 & b4 & b5 & b6 & b7;

        return AjaxResult.success("更新商品成功");
    }

    /**
     * 10.根据商品id获取商品编辑信息
     *
     * @param id
     * @return
     */
    @Override
    public AjaxResult updateInfoWithId(Integer id) {
        //获取基本的pmsProduct信息
        PmsProduct pmsProduct = pmsProductMapper.selectById(id);
        //创建封装的对象
        PmsProductDto pmsProductDto = new PmsProductDto();
        BeanUtils.copyProperties(pmsProduct, pmsProductDto);
        //获取子集

        List<PmsMemberPrice> pmsMemberPrice = pmsMemberPriceService.getWithId(id);
        List<CmsPrefrenceAreaProductRelation> cmsPrefrenceAreaProductRelation = cmsPrefrenceAreaProductRelationService.getWithId(id);
        List<PmsProductAttributeValue> pmsProductAttributeValue = pmsProductAttributeValueService.getWithId(id);
        List<PmsProductFullReduction> pmsProductFullReduction = pmsProductFullReductionService.getWithId(id);
        List<PmsProductLadder> pmsProductLadder = pmsProductLadderService.getWithId(id);
        List<PmsSkuStock> pmsSkuStock = pmsSkuStockService.getWithId(id);
        List<CmsSubjectProductRelation> cmsSubjectProductRelation = cmsSubjectProductRelationService.getWithId(id);

        pmsProductDto.setMemberPriceList(pmsMemberPrice);
        pmsProductDto.setPrefrenceAreaProductRelationList(cmsPrefrenceAreaProductRelation);
        pmsProductDto.setProductAttributeValueList(pmsProductAttributeValue);
        pmsProductDto.setProductFullReductionList(pmsProductFullReduction);
        pmsProductDto.setProductLadderList(pmsProductLadder);
        pmsProductDto.setSkuStockList(pmsSkuStock);
        pmsProductDto.setSubjectProductRelationList(cmsSubjectProductRelation);

        return AjaxResult.success(pmsProductDto);
    }

    //根据商品id获取到商品信息
    @Override
    public PmsProduct getById(Long productId) {

        PmsProduct pmsProduct = pmsProductMapper.selectById(productId);
        return pmsProduct;
    }
}
